<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelform library
jimport('joomla.application.component.modelitem');
 
/**
 * HelloWorld Model
 */
class ForumModelTopic extends JModelItem
{
	protected $_item = null;
	/**
	 * Method override to check if you can edit an existing record.
	 *
	 * @param	array	$data	An array of input data.
	 * @param	string	$key	The name of the key for the primary key.
	 *
	 * @return	boolean
	 * @since	1.6
	 */
	protected function allowEdit($data = array(), $key = 'id')
	{
		// Check specific edit permission then general edit permission.
		return JFactory::getUser()->authorise('core.edit', 'com_forum.topic.'.((int) isset($data[$key]) ? $data[$key] : 0)) or parent::allowEdit($data, $key);
	}
	

	/**
	 * Method to get an ojbect.
	 *
	 * @param	integer	The id of the object to get.
	 *
	 * @return	mixed	Object on success, false on failure.
	 */
	public function &getItem($id = null)
	{
		if ($this->_item === null)
		{
			$this->_item = false;

			if (empty($id)) {
				$id = $this->getState('topic.id');
			}

			// Get a level row instance.
			$table = JTable::getInstance('Topic', 'ForumTable');

			// Attempt to load the row.
			if ($table->load($id))
			{
				// Check published state.
				if ($published = $this->getState('filter.published'))
				{
					if ($table->state != $published) {
						return $this->_item;
					}
				}

				// Convert the JTable to a clean JObject.
				$properties = $table->getProperties(1);
				$this->_item = JArrayHelper::toObject($properties, 'JObject');
			}
			elseif ($error = $table->getError()) {
				$this->setError($error);
			}
		}

		return $this->_item;
	}	
	
	
	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param	type	The table type to instantiate
	 * @param	string	A prefix for the table class name. Optional.
	 * @param	array	Configuration array for model. Optional.
	 * @return	JTable	A database object
	 * @since	1.6
	 */
	public function getTable($type = 'Topic', $prefix = 'ForumTable', $config = array()) 
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	
	/**
	 * Method to get the script that have to be included on the form
	 *
	 * @return string	Script files
	 */
	public function getScript() 
	{
		return 'administrator/components/com_forum/models/forms/topic.js';
	}

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState()
	{
		$app = JFactory::getApplication();
		$params	= $app->getParams();

		// Load the object state.
		$id	= JRequest::getInt('id');
		$this->setState('topic.id', $id);
		
		$catid = JRequest::getInt('catid');
		$this->setState('category.id', $catid);

		// Load the parameters.
		$this->setState('params', $params);
	}	
	
	public function getPosts()
	{
		$tid = $this->getState('topic.id', 0);
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);

		// Select required fields from the categories.
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$limit = $this->getState('posts.limit', null);

		
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		// Select some fields
		$query->select('a.id,a.title,a.message,a.created,a.created_by,a.created_by_alias');
		// From the hello table
		$query->from('#__forum_posts AS a');
		$query->where('a.tid = '.$tid);
		// Join over the users for the author.
		$query->select('ua.name AS author_name');
		$query->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
		$query->order('a.id');
		
		$sql = (string)$query;
		
		if(!is_null($limit))
		{
			$sql .= " LIMIT " . $limitstart . ", " . $limit ;
		}
		
		$db->setQuery($sql);
		
	
		return $db->loadObjectList();
	
	}
}
